package net.wanho.systemSettings.controller;

import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.service.EmmPositionService;
import net.wanho.systemSettings.service.MenuService;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MenuServlet")
public class MenuServlet extends BaseServlet<MenuService> {

    EmmPositionService positionService = BeanFactory.getBean(EmmPositionService.class);
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methodName = request.getParameter("method");
        try {
            Method method = getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    protected String getAllMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            String menuname = request.getParameter("name");
            Map<String, String> condMap = new HashMap<>(1);
            if (StringUtils.isNotEmpty("menuname")) {
                condMap.put("menuname", menuname);
            }
            List<Menu> menulist = super.baseService.findByCond(condMap);
            request.setAttribute("menulist", menulist);
            return "/WEB-INF/view/systemSettings/menu/index.jsp";
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    protected String getAllParentMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        try {
            List<Menu> menuList = super.baseService.findAll();
            request.setAttribute("menuList", menuList);
            return "/WEB-INF/view/systemSettings/menu/create.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    protected AjaxResult addMenu(HttpServletRequest request, HttpServletResponse response) {
        try {
            Menu menu = super.parseRequest(request, Menu.class);
            super.baseService.save(menu);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }
   protected AjaxResult checkDeleteMenuId(HttpServletRequest request, HttpServletResponse response)
   {
       try {
           String menuId = request.getParameter("menuId");
           Menu menu = super.baseService.findById(menuId);
           if(menu.getParentMenuId()!=null)
           {
                return AjaxResult.fail("该菜单有存在上级关联节点");
           }
           return AjaxResult.success();
       } catch (ServiceException e) {
           e.printStackTrace();
           return AjaxResult.fail("删除失败！");
       }
   }

    protected void deleteMenuById(HttpServletRequest request, HttpServletResponse response) {
        try {
            String menuId = request.getParameter("menuId");
            super.baseService.remove(menuId);
            response.sendRedirect(request.getContextPath() + "/MenuServlet?method=getAllMenu");
        } catch (IOException | ServiceException e) {
            e.printStackTrace();
        }

    }

    protected String getUpdateMenudetailById(HttpServletRequest request, HttpServletResponse response) throws  ServiceException {
        try {
            String menuId = request.getParameter("menuId");
            Menu menu = super.baseService.findById(menuId);
            request.setAttribute("menu", menu);
            List<Menu> parentmenuList = super.baseService.findAll();
            request.setAttribute("parentmenuList", parentmenuList);
            return "/WEB-INF/view/systemSettings/menu/update.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }

    protected AjaxResult updateMenu(HttpServletRequest request, HttpServletResponse response) {
        //换种写法玩玩
        try {
            String menuid = request.getParameter("menuId");
            String menuname = request.getParameter("menu-name");
            String menuurl = request.getParameter("menu-url");
            String menuicon = request.getParameter("menu-icon");
            int id = Integer.parseInt(menuid);
            Menu menu = new Menu();
            menu.setMenuId(id);
            menu.setMenuName(menuname);
            menu.setMenuUrl(menuurl);
            menu.setPictures(menuicon);
            super.baseService.modify(menu);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }
}
